package com.yrh.controller;

import com.yrh.bean.TCart;
import com.yrh.bean.TOrder;
import com.yrh.bean.TOrderDetails;
import com.yrh.bean.TUser;
import com.yrh.service.TCartService;
import com.yrh.service.TOrderDetailsService;
import com.yrh.service.TOrderService;
import com.yrh.service.impl.TCartServiceImpl;
import com.yrh.service.impl.TOrderDetailsServiceImpl;
import com.yrh.service.impl.TOrderServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/*订单控制层*/
@WebServlet("/OrderServlet/*")
public class OrderServlet extends BaseServlet {
    private TOrderService tOrderService=new TOrderServiceImpl();
    private TOrderDetailsService tOrderDetailsService=new TOrderDetailsServiceImpl();
    //去订单页面
    public void gotoOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台的参数封装为JavaBean
        Map<String, String[]> map = request.getParameterMap();
        TOrder tOrder = new TOrder();
        BeanUtils.populate(tOrder,map);
        //获取用户数据
        TUser user = (TUser) request.getSession().getAttribute("user");
        String userRole = user.getUserRole();
        int userId = user.getUserId();
        //查询数据
        List<TOrder> tOrders = tOrderService.allOrder(tOrder, userRole,userId,0, 0);
        //分页操作
        int size=2;//每页的数据量
        int totalPage=tOrders.size()%size==0?tOrders.size()/size:tOrders.size()/size+1;//计算出多少页
        int nowPage=1;//默认显示第一页;
        if (request.getParameter("nowPage")!=null) {
            nowPage = Integer.parseInt(request.getParameter("nowPage"));
        }
        tOrders=tOrderService.allOrder(tOrder,userRole,userId, nowPage, size);
        double sum=0;//计算总金额
        for (TOrder order : tOrders){
            List<TOrderDetails> tOrderDetails = tOrderDetailsService.byOrderId(order.getOrderId());
            for (TOrderDetails tOrderDetail : tOrderDetails) {
               sum+=tOrderDetail.getProductNum()*tOrderDetail.getProductMoney();
            }
            order.setSum(sum);
            sum=0;
            order.settOrderDetails(tOrderDetails);
        }
        request.setAttribute("nowPage",nowPage);
        request.setAttribute("totalPage",totalPage);
        request.setAttribute("oList",tOrders);
        request.setAttribute("addTime",tOrder.getAddTime());
        request.setAttribute("updateTime",tOrder.getUpdateTime());
        request.setAttribute("orderType",tOrder.getOrderType());
        request.setAttribute("orderId",tOrder.getOrderId());
        request.getRequestDispatcher("/order.jsp").forward(request,response);
    }
    //修改订单状态
    public void updateType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台的参数封装为JavaBean
        Map<String, String[]> map = request.getParameterMap();
        TOrder tOrder = new TOrder();
        BeanUtils.populate(tOrder,map);
        System.out.println("tOrder = " + tOrder);
        int i = tOrderService.updateType(tOrder);
        if (i>0){
            response.getWriter().println(1);
        }else {
            response.getWriter().println(-1);
        }
    }
    //提交订单
    public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //购物车业务层对象
         TCartService service=new TCartServiceImpl();
         //获取用户数据
        TUser user = (TUser) request.getSession().getAttribute("user");
        TCart cart = new TCart();
        cart.setUserId(user.getUserId());
        //获取购物车的数据
        List<TCart> list = service.allCart(cart);
        System.out.println("list = " + list);
        //清空购物车
        int i = service.deleteCart(cart);
        TOrder order = new TOrder();
        order.setUserId(user.getUserId());
        //获取收货地址
        String address = request.getParameter("address");
        order.setAddressDetails(address);
        //生成订单
        int order1 = tOrderService.addOrder(order);
        //得到创建的订单号
        int oid = tOrderService.maxOrderId();
        //根据生成的订单号和购物车数据生成订单详情表
        if (list.size()>0){
            for (TCart tCart : list) {
                TOrderDetails todersde = new TOrderDetails();
                //商品名
                todersde.setProductName(tCart.getProductName());
                //商品数量
                todersde.setProductNum(tCart.getProductNum());
                //商品价格
                todersde.setProductMoney(tCart.getProductPrice());
                //订单id
                todersde.setOrderId(oid);
                int i1 = tOrderDetailsService.addOrderDetail(todersde);
                if (i1<0){
                    System.out.println("\"错误\" = " + "错误");
                    return;
                }
            }
            //添加成功信息
            response.getWriter().println(1);
        }

    }
}
